package com.hzlj.position.locate.dao;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fz.common.base.converter.QueryWrapperConverterPlus;
import com.fz.common.base.dto.PageParamDTO;
import com.fz.common.base.dto.QueryPageResultDTO;
import com.fz.common.base.dto.SortParamDTO;
import com.hzlj.position.locate.common.dto.reportInspectionArtificial.ReportInspectionArtificialQueryParamDTO;
import com.hzlj.position.locate.common.dto.reportInspectionArtificial.ReportInspectionArtificialWithJzJbxxDTO;
import com.hzlj.position.locate.common.model.ReportInspection;
import com.hzlj.position.locate.common.model.ReportInspectionArtificial;
import com.hzlj.position.locate.converter.ReportInspectionConverter;
import com.hzlj.position.locate.mapper.ReportInspectionArtificialMapper;
import com.hzlj.sqjz.jz.common.model.JzJbxx;
import com.hzlj.sqjz.jz.mapper.JzJbxxMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 通信联络核查-核查-手工(ReportInspectionArtificial)表操作类
 *
 * @author lifh
 * @date 2023-07-25 10:14:09
 */
@Repository
public class ReportInspectionArtificialDao extends ServiceImpl<ReportInspectionArtificialMapper, ReportInspectionArtificial> {
    @Resource
    private JzJbxxMapper jzJbxxMapper;

    public boolean updateByInspection(ReportInspection inspection) {
        LambdaUpdateWrapper<ReportInspectionArtificial> queryWrapper = new UpdateWrapper<ReportInspectionArtificial>().lambda()
                .eq(ReportInspectionArtificial::getInspectionId, inspection.getId())
                .eq(ReportInspectionArtificial::getId, inspection.getJzId());
        return this.update(
                ReportInspectionConverter.toArtificial(inspection),
                queryWrapper
        );
    }

    public void updateByInspections(List<ReportInspection> inspections) {
        if (ObjectUtil.isEmpty(inspections)) {
            return;
        }
        for (ReportInspection inspection : inspections) {
            this.updateByInspection(inspection);
        }
    }

    public void upsert(List<ReportInspection> inspections,
                       String creatorJgCode,
                       String creatorJgMc) {
        if (ObjectUtil.isEmpty(inspections)) {
            return;
        }
        List<ReportInspectionArtificial> artificials = inspections.stream().map(ReportInspectionConverter::toArtificial).collect(Collectors.toList());
        for (ReportInspectionArtificial artificial : artificials) {
            artificial.setCreatorJgCode(creatorJgCode);
            artificial.setCreatorJgMc(creatorJgMc);
            artificial.setInspectionAmt(1);
            ReportInspectionArtificial exists = this.getById(artificial.getId());
            //更新
            if (ObjectUtil.isNotEmpty(exists)) {
                artificial.setInspectionAmt(ObjectUtil.defaultIfNull(exists.getInspectionAmt(), 0) + 1);
                this.updateById(artificial);
                continue;
            }
            this.save(artificial);
        }
    }


    /**
     * 查询列表：不分页（人工点验）
     */
    public List<ReportInspectionArtificialWithJzJbxxDTO> listReportArtificial(ReportInspectionArtificialQueryParamDTO dto) {
        return reportArtificialQuery()
                .list(ReportInspectionArtificialWithJzJbxxDTO.class, dto, sortParam());
    }

    /**
     * 查询列表：分页（人工点验）
     */
    public QueryPageResultDTO<ReportInspectionArtificialWithJzJbxxDTO> pageReportArtificial(ReportInspectionArtificialQueryParamDTO dto, PageParamDTO page) {
        page.setSortFns(sortParam().getSortFns());
        return reportArtificialQuery()
                .page(ReportInspectionArtificialWithJzJbxxDTO.class, dto, page);
    }


    private QueryWrapperConverterPlus<JzJbxx> reportArtificialQuery() {
        return QueryWrapperConverterPlus.of(jzJbxxMapper)
                .leftJoin(ReportInspectionArtificial.class, on -> on.eq(ReportInspectionArtificial::getId, JzJbxx::getId));
    }


    private SortParamDTO sortParam() {
        return SortParamDTO
                .of(false, true, ReportInspectionArtificial::getReportTime)
                .and(false, true, ReportInspectionArtificial::getInspectionId)
                .and(false, JzJbxx::getId);
    }
}

